﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Manager_Members : System.Web.UI.PageEx
{
    private string strPageCode = "M00001";
    private string strLoginResult = string.Empty;
    private int iLoginUserId = 0;

    private int iPageCount = 0;
    private int iRecordCount = 0;
    private string strQuery = " and 1=1";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }

        int.TryParse(Convert.ToString(Session["UserId"]), out iLoginUserId);
        
        if (!Page.IsPostBack)
        {
            DataBinds();
            ddlPageSize.SelectedValue = gridList.PageSize.ToString();
            btnReset.OnClientClick = sfObject.GetResetReference();
        }
    }
    private void DataBinds()
    {
        DataBinds("AddTime", "desc");
    }
    private void DataBinds(string strSort, string strSortDirection)
    {
        int iSortDirection = 1;
        strSortDirection = strSortDirection.ToLower();
        switch (strSortDirection)
        { 
            case "desc":
                iSortDirection = 1;
                break;
            default:
                iSortDirection = 0;
                break;
        }

        strQuery += Convert.ToString(ViewState["Where"]);
        DataSet ds = WebBase.Comm.SelectByPage("dbUsers", "*", strQuery, strSort, "UserId", 0, iSortDirection, gridList.PageSize, gridList.PageIndex + 1, out iPageCount, out iRecordCount);
        gridList.RecordCount = iRecordCount;
        gridList.DataSource = ds;
        gridList.DataBind();
    }
    protected void gridList_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {
        gridList.PageIndex = e.NewPageIndex;
        DataBinds();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        DataBinds();
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        int iPageSize = gridList.PageSize;
        int.TryParse(ddlPageSize.SelectedValue, out iPageSize);
        gridList.PageSize = iPageSize;
        DataBinds();
    }
    protected void gridList_Sort(object sender, FineUI.GridSortEventArgs e)
    {
        DataBinds(e.SortField, e.SortDirection);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        winSearch.Hidden = false;
    }
    protected void btnSearch1_Click(object sender, EventArgs e)
    {
        winSearch.Hidden = true;
        string strWhere = string.Empty;
        string strUserName = SqlHelper.String(txtUserName.Text);
        string strRegDate1 = dpRegDate1.Text;
        string strRegDate2 = dpRegDate2.Text;
        string strLogdate1 = dpLoginDate1.Text;
        string strLogdate2 = dpLoginDate2.Text;
        if (!string.IsNullOrEmpty(strUserName))
            strWhere += " and UserName='{0}'";
        if (!string.IsNullOrEmpty(strRegDate1))
            strWhere += " and Convert(varchar(10),AddTime)>='{1}'";
        if (!string.IsNullOrEmpty(strRegDate2))
            strWhere += " and Convert(varchar(10),AddTime)<='{2}'";
        if (!string.IsNullOrEmpty(strLogdate1))
            strWhere += " and Convert(varchar(10),LastLoginTime)>='{3}'";
        if (!string.IsNullOrEmpty(strLogdate2))
            strWhere += " and Convert(varchar(10),LastLoginTime)<='{4}'";
        strWhere = string.Format(strWhere, new object[] { 
            strUserName,
            strRegDate1,
            strRegDate2,
            strLogdate1,
            strLogdate2
        });
        ViewState["Where"] = strWhere;
        DataBinds();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string strResult = string.Empty;
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        foreach (int iSelectId in lstSelectRowId)
        {
            DalEx.DbUserDalEx.Delete(" and UserId=" + iSelectId, ref strResult);
        }
        FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
        DataBinds();
    }
    protected string GetSex(object objValue)
    {
        int iValue = Convert.ToInt32(objValue);
        return iValue == 1 ? "男" : "女";
    }
}
